home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / dtk_demo.zip / PR_SPOOL.C < prev    next >
C/C++ Source or Header  |  1991-09-12  |  6KB  |  210 lines

  1. /*  PR_SPOOL.C
  2.  *  demonstrates interface to print spooler
  3.  *  last mod.: 09-SEP-91
  4.  */
  5.  
  6. #include <STDIO.H>
  7. #include <STDLIB.H>
  8. #include <CONIO.H>
  9.  
  10. #include <L_PRINT.H>
  11. #include <L_STR.H>
  12. #include <L_DIR.H>
  13.  
  14. Uchar *menu_letters = "SCAHERQ";
  15.  
  16. Uchar *menu =
  17. "\nS Submit a file    C Cancel a file (wildcards OK)   A Cancel all files\n"
  18.   "H Hold the queue   E End the hold   R Redisplay queue   Q Quit   --> ";
  19.  
  20. Far_str_ptr file_ptrs[_MAX_SPOOL_FILES_];
  21. Uchar filepath[_MAX_PATH_];
  22.  
  23. void main(int argc, char **argv);
  24. void report_error(int err_num, Str_ptr when);
  25. void display_print_queue(int num_files);
  26.  
  27. /*----------------------------*/
  28. void main(int argc, char **argv)
  29. {
  30. int i, result, num_files, qnhold=FALSE, which;
  31. Uchar ch=0;
  32.  
  33. result = print_spool_status();
  34.  
  35. switch ( result )
  36.     {
  37.     case 0:
  38.         printf("Print spooler is installed.\n");
  39.         break;
  40.     case -21:
  41.         printf("Print spooler is not installed.\n");
  42.         break;
  43.     case -22:
  44.         printf("Print spooler cannot be installed.\n");
  45.         exit(-22);
  46.     case -23:
  47.         printf("DOS version earlier than 3.0.\n");
  48.         exit(-23);
  49.     default:
  50.         printf("print_spool_status() returns error %d.\n",result);
  51.         exit(result);
  52.     }
  53.  
  54. if ( result == -21 )
  55.     {
  56.     printf("Install print spooler? (Y/N) ");
  57.     while ( ch != 'Y' && ch != 'N' )
  58.         {
  59.         ch = (Uchar)getch();
  60.         ch -= 32*(ch>'Z');
  61.         }
  62.     printf("%c\n",ch);
  63.     if ( ch == 'N' )
  64.         exit(0);
  65.  
  66.     result = print_spool_install(&which);
  67.     if ( result != 0 )
  68.         {
  69.         report_error(result,"installing print spooler");
  70.         exit(0);
  71.         }
  72.     else
  73.         printf("PRINT.%s installed.\n",(which==1?"EXE":"COM"));
  74.     }
  75.  
  76. if ( argc > 1 )
  77.     {
  78.     for ( i=1; i<argc; i++ )
  79.          print_spool_submit_file(argv[i]);
  80.          /*  ignore errors  */
  81.     }
  82.  
  83. while ( TRUE )
  84.     {
  85.     /*  check files in the print queue  */
  86.     num_files = print_spool_hold(NULL);
  87.     if ( num_files < 0 )
  88.         {
  89.         report_error(num_files,"holding the queue");
  90.         exit(num_files);
  91.         }
  92.     else
  93.         {
  94.         printf("\n%d file%s in print queue:\n",
  95.             num_files, ( num_files==1 ? "" : "s" ) );
  96.         if ( num_files > 0 )
  97.             {
  98.             num_files = print_spool_hold(file_ptrs);
  99.             if ( num_files < 0 )
  100.                 {
  101.                 report_error(num_files,"holding the queue");
  102.                 exit(num_files);
  103.                 }
  104.             else
  105.                 {
  106.                 display_print_queue(num_files);
  107.                 if ( qnhold )
  108.                     printf("Print queue on hold.\n");
  109.                 }
  110.             }
  111.         }
  112.     if ( !qnhold )
  113.         print_spool_end_hold();
  114.     printf(menu);
  115.     ch = -1;
  116.     while ( ( i = char_at(menu_letters,ch,0) ) == -1 )
  117.         {
  118.         ch = (Uchar)getch();
  119.         ch -= 32*(ch>'Z');
  120.         }
  121.     printf("%c\n",ch);
  122.     if ( ch == 'Q' )
  123.         break;          /*  quit  */
  124.     switch ( i )
  125.         {
  126.         case 0:     /*  submit a file  */
  127.             /*  allow maximum input of _MAX_PATH_ - 4 characters  */
  128.             printf("File: ");
  129.             gets_n(filepath,_MAX_PATH_-4);
  130.             putchar('\n');
  131.             trim(filepath);
  132.             if ( *filepath )
  133.                 {
  134.                 result = print_spool_submit_file(filepath);
  135.                 if ( result < 0 )
  136.                     report_error(result,"submitting file");
  137.                 qnhold = FALSE;
  138.                 }
  139.             break;
  140.         case 1:     /*  cancel a file  */
  141.             printf("File: ");
  142.             gets_n(filepath,_MAX_PATH_-4);
  143.             putchar('\n');
  144.             trim(filepath);
  145.             if ( *filepath )
  146.                 {
  147.                 result = print_spool_cancel_file(filepath);
  148.                 if ( result < 0 )
  149.                     report_error(result,"cancelling file");
  150.                 qnhold = FALSE;
  151.                 }
  152.             break;
  153.         case 2:     /*  cancel all files  */
  154.             print_spool_cancel_all();
  155.             if ( result < 0 )
  156.                 report_error(result,"cancelling all files");
  157.             qnhold = FALSE;
  158.             break;
  159.         case 3:     /*  hold the queue  */
  160.             qnhold = TRUE;
  161.             break;
  162.         case 4:     /*  end the hold  */
  163.             qnhold = FALSE;
  164.         }
  165.     }
  166. }
  167.  
  168. /*--------------------------*/
  169. void report_error(int err_num,
  170.                   Str_ptr when)
  171. {
  172. switch ( err_num )
  173.     {
  174.     case  -1: printf("Function invalid");  break;
  175.     case  -2: printf("File not found");  break;
  176.     case  -3: printf("Path not found");  break;
  177.     case  -4: printf("Too many open files");  break;
  178.     case  -5: printf("Access denied");  break;
  179.     case  -8: printf("Print queue full");  break;
  180.     case  -9: printf("Spooler busy");  break;
  181.     case -12: printf("Name too long");  break;
  182.     case -15: printf("Drive invalid");  break;
  183.     case -20: printf("Wildcards"); break;
  184.     case -32: printf("Spooler not found"); break;
  185.     case -37: printf("Environment too big"); break;
  186.     case -38: printf("Spooler not executable"); break;
  187.     case -42: printf("Insufficient memory"); break;
  188.     default:  printf("%d",err_num);
  189.     }
  190. printf(" error when %s.\n",when);
  191. }
  192.  
  193. /*-----------------------------------*/
  194. void display_print_queue(int num_files)
  195. {
  196. int i;
  197. Far_str_ptr ptr;
  198.  
  199. for ( i=0; i<num_files; i++ )
  200.     {
  201.     /*  file_ptrs[i] is a Far_str_ptr
  202.      *  so can't use printf("%s",file_ptrs[i])
  203.      */
  204.     ptr = file_ptrs[i];
  205.     while ( *ptr )
  206.         putchar(*ptr++);
  207.     putchar('\n');
  208.     }
  209. }
  210.